package com.example.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ExecutionContext {
    static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal();

    private ExecutionContext() {
    }

    public static Map<String, Object> getExecutionContextMap() {
        Map<String, Object> result = (Map)threadLocal.get();
        return result;
    }

    public static void setExecutionContextMap(Map<String, Object> executionContextMap) {
        threadLocal.set(executionContextMap);
    }

    public static Object put(String key, Object value) {
        Map<String, Object> executionContextMap = getExecutionContextMap();
        if (executionContextMap == null) {
            executionContextMap = new HashMap();
            setExecutionContextMap((Map)executionContextMap);
        }

        return ((Map)executionContextMap).put(key, value);
    }

    public static Object get(String key) {
        Map<String, Object> executionContextMap = getExecutionContextMap();
        return executionContextMap == null ? null : executionContextMap.get(key);
    }

    /*public static SysUser getUserInfo() {
        Map<String, Object> executionContextMap = getExecutionContextMap();
        return executionContextMap == null ? null : (SysUser)executionContextMap.get("sysUser");
    }

    public static List<Integer> getRoleIdList() {
        Map<String, Object> executionContextMap = getExecutionContextMap();
        if (executionContextMap == null) {
            return null;
        } else {
            List<Integer> roleIds = new ArrayList();
            List<SysRole> sysRoles = getUserInfo().getRoleList();
            if (sysRoles != null && !sysRoles.isEmpty()) {
                roleIds = (List)sysRoles.stream().map((m) -> {
                    return m.getRoleId();
                }).collect(Collectors.toList());
            }

            return (List)roleIds;
        }
    }

    public static List<Integer> departmentIdList() {
        Map<String, Object> executionContextMap = getExecutionContextMap();
        if (executionContextMap == null) {
            return null;
        } else {
            List<Integer> departmentIdIds = new ArrayList();
            List<SysDepartment> departmentList = getUserInfo().getDepartmentList();
            if (departmentList != null && !departmentList.isEmpty()) {
                departmentIdIds = (List)departmentList.stream().map((m) -> {
                    return m.getDepartmentId();
                }).collect(Collectors.toList());
            }

            return (List)departmentIdIds;
        }
    }*/
}
